package com.base.data.elasticsearch.core.syncmapping;

import org.elasticsearch.action.CompositeIndicesRequest;
import org.elasticsearch.action.DocWriteRequest;
import org.elasticsearch.action.support.replication.ReplicatedWriteRequest;
import org.elasticsearch.common.bytes.BytesArray;
import org.elasticsearch.common.bytes.BytesReference;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.common.xcontent.XContentHelper;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.index.VersionType;

import java.io.IOException;
import java.io.UncheckedIOException;
import java.util.Map;
import java.util.Objects;

public class BaseCreateIndexRequest extends ReplicatedWriteRequest<BaseCreateIndexRequest> implements DocWriteRequest<BaseCreateIndexRequest>, CompositeIndicesRequest {

    private BytesReference source;
    private XContentType contentType;

    private String type;

    private OpType opType = OpType.INDEX;

    public BytesReference source() {
        return source;
    }



    public XContentType getContentType() {
        return contentType;
    }

    public BaseCreateIndexRequest type(String type) {
        this.type = type;
        return this;
    }

    public BaseCreateIndexRequest create(boolean create) {
        if (create) {
            return opType(OpType.INDEX);//put
        } else {
            return opType(OpType.UPDATE);//post
        }
    }

    public BaseCreateIndexRequest opType(OpType opType) {
        if (opType != OpType.UPDATE && opType != OpType.INDEX) {
            throw new IllegalArgumentException("opType must be 'create' or 'index', found: [" + opType + "]");
        }
        this.opType = opType;
        return this;
    }

    public BaseCreateIndexRequest source(XContentBuilder sourceBuilder)  {
        this.source = Objects.requireNonNull(sourceBuilder.bytes());
        this.contentType = Objects.requireNonNull(sourceBuilder.contentType());
        return this;
    }

    /**
     * just support XContentType.JSON
     * @param mappingSource
     * @return
     */
    public BaseCreateIndexRequest source(String mappingSource) {
        this.source = new BytesArray(mappingSource);
        this.contentType = Objects.requireNonNull(XContentType.JSON);
        return this;
    }

    /**
     * just support XContentType.JSON
     * @param mappingSource
     * @return
     */
    public BaseCreateIndexRequest source(Map mappingSource) throws IOException {
        XContentBuilder builder = XContentFactory.contentBuilder(XContentType.JSON);
        builder.map(mappingSource);
        this.source = builder.bytes();
        this.contentType = Objects.requireNonNull(XContentType.JSON);
        return this;
    }


    //    = Strings.hasLength(indexRequest.id()) ? HttpPut.METHOD_NAME : HttpPost.METHOD_NAME;
    @Override
    public String type() {
        return this.type;
    }

    @Override
    public String id() {
        return null;
    }

    @Override
    public BaseCreateIndexRequest routing(String routing) {
        return null;
    }

    @Override
    public String routing() {
        return null;
    }

    @Override
    public String parent() {
        return null;
    }

    @Override
    public long version() {
        return 0;
    }

    @Override
    public BaseCreateIndexRequest version(long version) {
        return null;
    }

    @Override
    public VersionType versionType() {
        return VersionType.INTERNAL;
    }

    @Override
    public BaseCreateIndexRequest versionType(VersionType versionType) {
        return null;
    }

    @Override
    public OpType opType() {
        return this.opType;
    }
}
